﻿using System;
using NHibernate;
using NUnit.Framework;
using ScrumBox.DataAccess.Repositories;
using ScrumBox.IntegrationTests.DataAccess;
using ScrumBox.Model;

namespace ScrumBox.IntegrationTests
{
    [TestFixture]
    public class SprintRepositoryTester : DatabaseTesterBase
    {
        [Test]
        public void CanSaveSprint()
        {
            var project = new Project("My Project", "MyProject", DateTime.Now);
            var projectRepository = new ProjectRepository(_sessionBuilder);
            projectRepository.Save(project);
            
            var repository = new SprintRepository(_sessionBuilder);
            var sprintStart = DateTime.Now;
            var sprintEnd = DateTime.Now + TimeSpan.FromDays(14);
            var sprintTitle = "MyNewSprint";
            var sprintKey = string.Format("{0}-{1}", project.Key, sprintTitle);
            var sprint = new Sprint(sprintKey, sprintTitle, sprintStart, sprintEnd);
            
            repository.Save(sprint);
            

            using (var session = _sessionBuilder.GetSession())
            {
                session.Clear();
                var sprintToEdit = repository.GetByKey(sprintKey);
                Assert.That(sprintToEdit.Id == sprint.Id, "Id should be the same.");
                Assert.That(sprintToEdit.Key == sprint.Key, "Project key should be the same.");

                //TODO: Check for a better way to compare the dates.
                Assert.That(sprintToEdit.StartDate.Value.ToLongDateString() == sprint.StartDate.Value.ToLongDateString(),
                    "Start date should be the same.");
                Assert.That(sprintToEdit.EndDate.Value.ToLongDateString() == sprint.EndDate.Value.ToLongDateString(),
                    "End date should be the same.");
            }
        }
    }
}